package com.leyou.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

@Service
public class GoodsHtmlService {

    @Autowired
    private TemplateEngine engine;
    @Autowired
    private GoodsService service;

    public void createHtml(Long spuId) {
        //上下文
        Context context = new Context();
        context.setVariables(this.service.loadData(spuId));
        //流  ,在创建文件时莫忘记检查拼接的路径是否正确，在目录后追加//子目录
        File file = new File("D:\\nginx\\nginx-1.14.0\\html\\item\\" + spuId + ".html");
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
        }
        this.engine.process("item", context, printWriter);
    }
}
